const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');

module.exports = {
  entry: {
    index: './src/index.js'
  },
  plugins: [
    new HtmlWebpackPlugin({
      template: './src/html/index.html'
    }),
    new webpack.HashedModuleIdsPlugin(),
    new webpack.ProvidePlugin({
      join: ['lodash', 'join']
    }),
    new MiniCssExtractPlugin({
      filename: '[name].[contenthash].css'
    })
  ],

  module: {
    rules: [
      {test: require.resolve('./src/globals.js'), use: 'exports-loader?file,parse=helpers.parse'},

      {
        test: /\.(png|jpg|gif)$/i, use: {
          loader: 'url-loader',
          options: {
            limit: 8192,
            context: 'src',
            name: '[path][name].[hash:20].[ext]'
          }
        }
      },
      {
        test: /\.(html)$/, use: {
          loader: 'html-loader',
          options: {
            interpolate: 'require'
          }
        }
      },
      {
        test: /\.css$/, use: [
          'style-loader', {
            loader: 'css-loader'
          }
        ]
      },
      {
        test: /\.scss$/, use: [
          {
            loader: MiniCssExtractPlugin.loader
          },
          {
            loader: 'css-loader',
            options: {
              sourceMap: true
            }
          },
          'resolve-url-loader',
          {
            loader: 'sass-loader',
            options: {
              sourceMap: true,
              outputStyle: 'compact'
            }
          }
        ]
      }
    ]
  },
  output: {
    filename: '[name].[contenthash].js',
    path: path.resolve(__dirname, 'dist')
  },
  optimization: {
    runtimeChunk: 'single',
    splitChunks: {
      cacheGroups: {
        vendor: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendors',
          chunks: 'all'
        },
        styles: {
          name: 'styles',
          test: /\.css$/,
          chunks: 'all',
          enforce: true
        }
      }
    }
  }
};